package org.example;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;

public class Main {
    public static void main(String[] args) {
        // 创建Kafka生产者配置
        Properties props = new Properties();
        props.put("bootstrap.servers", "192.168.0.119:9092"); // 替换为你的Kafka集群地址
        props.put("key.serializer", StringSerializer.class.getName());
        props.put("value.serializer", StringSerializer.class.getName());

        // 创建Kafka生产者实例
        try (KafkaProducer<String, String> producer = new KafkaProducer<>(props)) {
            // 构建要发送的消息
            ProducerRecord<String, String> record = new ProducerRecord<>("test1", "key", "Hello, Kafka!");

            // 发送消息
            producer.send(record).get(); // 使用get()方法等待消息发送完成

            System.out.println("Message sent successfully.");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}